package com.baitiaojun.bulletscreen.util;

import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.util.SpringContextUtil;
import com.baitiaojun.bulletscreen.support.RedisSupport;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @类名 TokenUtil
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/2/27 17:48
 * @版本 1.0
 */
public class TokenUtil {

    public static String refreshToken(String accessToken) throws Exception {
        RedisSupport redisSupport = SpringContextUtil.getBean(RedisSupport.class);
        String refreshToken = String.valueOf(redisSupport.getHash(accessToken, ConstantUtil.Field.REFRESH_TOKEN.getKey()));
        if (com.baitiaojun.api.util.TokenUtil.isExpiresToken(refreshToken)) {
            Map<String, Object> values = redisSupport.hash().entries(accessToken);
            redisSupport.delete(accessToken);
            accessToken = com.baitiaojun.api.util.TokenUtil.generateToken(com.baitiaojun.api.util.TokenUtil.verifyToken(refreshToken), ConstantUtil.TokenType.ACCESS);
            redisSupport.hash().putAll(accessToken, values);
            redisSupport.getRedisTemplate().expire(accessToken, ConstantUtil.REFRESH_TOKEN_TIMEOUT, TimeUnit.DAYS);
        }
        return accessToken;
    }
}
